<?php
abstract class Calculator {
	protected abstract function cnd($x);
	
	public function Calculate($call_put, $S, $X, $T, $r, $v) {
		//math logic to be used in equation
		$sqrV = pow($v,2);
		$sqrtV = pow($v,0.5);
		$sqrtT = pow($T,0.5);
		$logSX = log($S/$X);
		$expRT = exp((0-$r)*$T);

		//d1 = ln(S/X) + (r + v^2/2)T / v * sqrt(T)
		$d1 = ( $logSX + ($r + $sqrV / 2) * $T ) / ( $v * $sqrtT );
		//d2 = d1 - v * sqrt(T)
		$d2 = $d1 - ($v * $sqrtT);

		if ($call_put == 'c') {
			//if call, c = S * cnd(d1) - X*exp((0-r)*T)) * cnd(d2)
			return ($S * $this->cnd($d1)) - ($X * $expRT * $this->cnd($d2));
		} else { 
			//if put, p = X * exp((0-r)*T)) * cnd(-d2) - S*cnd(-d1)
			return ($X * $expRT * $this->cnd(-$d2)) - ($S * $this->cnd(0-$d1));
		}
	}
}
?>